﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using LanhuFx.Services.Model.Dto;
using LanhuFx.Services.Model.View;
using LanhuFx.Identity;
using LanhuFx.Context.PgSql.ResourcesModels;

namespace LanhuFx.Services
{
    public class UserService
    {
        private readonly LanhuFxContext _Context;
        private readonly IMapper _mapper;
        private readonly TokenService _tokenService;

        public UserService(LanhuFxContext arg_Context
            , IMapper arg_mapper, TokenService arg_tokenService)
        {
            _Context = arg_Context;
            _tokenService = arg_tokenService;
            _mapper = arg_mapper;
        }

        /// <summary>
        /// 根据用户获取角色信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<View_RoleList> RoleList(Guid userId)
        {
            var data = _Context.IdentityUserrole.Where(c => c.Userid == userId).ToList();

            var roleList = new List<View_RoleList>();

            if (!data.Any())
            {
                return new List<View_RoleList>();
            }
            foreach (var item in data)
            {
                var roleId = _Context.IdentityRole.FirstOrDefault(c => c.Id == item.Roleid);

                if (roleId != null)
                {
                    var role = new View_RoleList
                    {
                        Id = roleId.Id,
                        Name = roleId.Name
                    };
                    roleList.Add(role);
                }
            }
            return roleList;
        }

        /// <summary>
        /// 获取所有角色信息
        /// </summary>
        /// <returns></returns>
        public List<View_RoleList> AllRoleList()
        {
            return _Context.IdentityRole.Select(c => new View_RoleList
            {
                Id = c.Id,
                Name = c.Name,
                SystemCode = c.Systemcode
            }).ToList();
        }

        /// <summary>
        /// 设置用户角色
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public string SetUserRole(Dto_SetUserRole arg)
        {
            var session = _tokenService.GetToken();
            var data = _Context.IdentityUserrole.Include(c => c.Role).Where(c => c.Userid == arg.UserId);
            string beforeRoles = "";
            if (data.Any())
            {
                beforeRoles = string.Join(",", data.Select(c => c.Role.Name));
                _Context.IdentityUserrole.RemoveRange(data);
            }
            arg.RoleId.ForEach(item =>
            {
                _Context.IdentityUserrole.Add(new IdentityUserRole
                {
                    Roleid = item,
                    Userid = arg.UserId,
                    Teamid = session.TeamId
                });
            });
            var roles = _Context.IdentityRole.Where(c => arg.RoleId.Contains(c.Id)).Select(c => c.Name).ToList();
            var afterroles = string.Join(",", roles);

            _Context.SaveChanges();
            return "设置成功";
        }

    }
}
